<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeItem
          :tree-node="company"
          is-root
          @update-isShowAddDept="AddDept"
        />
        <!-- 树形控件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template slot-scope="{ data }">
            <TreeItem
              :tree-node="data"
              @update-delList="getDepartments"
              @update-isShowAddDept="AddDept"
              @update-edit="editDept"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="AddDept"
      :is-show-add-dept.sync="isShowAddDept"
      :tree-node="treeNode"
      @update-addDept="getDepartments"
    />
  </div>
</template>

<script>
import TreeItem from './components/tree-item'
import AddDept from './components/add-dept'
import { tranListToTreeData } from '../../utils/index'
export default {
  components: {
    TreeItem, AddDept
  },
  data() {
    return {
      list: [],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      defaultProps: {
        label: 'label',
        children: 'children'
      },
      isShowAddDept: false,
      treeNode: {},
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    editDept(treeNode) {
      this.isShowAddDept = true
      this.treeNode = treeNode
      // 调用子组件adddept方法
      this.$refs.AddDept.getDepartDetail(treeNode.id)
    },
    AddDept(treeNode) {
      this.isShowAddDept = true
      this.treeNode = treeNode
    },
    async getDepartments() {
      this.loading = true
      const res = await this.apis.departments.getDepartments()
      // console.log(res)
      // 请求回来的res.depts数据只有一层，需要转换成树型结构
      this.list = tranListToTreeData(res.depts, '')
      this.loading = false
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
